﻿#region Using Statements
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace ObjectComposition
{
	public class Main : Microsoft.Xna.Framework.Game
	{
		public List<Entity> Entities { get; protected set; }

		public void Add(Entity entity)
		{
			this.Entities.Add(entity);
		}

		public void Remove(Entity entity)
		{
			this.Entities.Remove(entity);
		}

		public IEnumerable<Entity> Get(Entity.EntityType type)
		{
			return this.Entities.Where(x => x.Type == type);
		}

		public Entity Get(string id)
		{
			return this.Entities.FirstOrDefault(x => x.ID == id);
		}

		public Main()
		{
			this.graphics = new GraphicsDeviceManager(this);

			this.Content = new ContentManager(this.Services);
			this.Content.RootDirectory = "Content";
			
			this.Entities = new List<Entity>();
		}
 
		protected override void LoadContent()
		{
#if WINDOWS
			this.graphics.SynchronizeWithVerticalRetrace = false;
			this.graphics.IsFullScreen = false;
			this.ResizeViewport(1280, 720);
			this.IsMouseVisible = false;
#endif
		}

		protected override void Update(GameTime gameTime)
		{
			if (Keyboard.GetState().IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
				this.Exit();

			foreach (Entity entity in this.Entities.ToList())
				entity.Update(gameTime);

			base.Update(gameTime);
		}

		protected override void Draw(GameTime gameTime)
		{
			foreach (Entity entity in this.Entities.OrderBy(x => x.DrawOrder))
				entity.Draw(gameTime);
		}
	}
}